<template>
  <div class="login_container">
    <!-- 主题头像区域 -->
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 登陆框 -->
      <el-form label-width="80px" class="login-form" :model="loginForm" ref="loginFormRef">
        <!-- 用户名 -->
        <el-form-item label="用户名:" prop="account">
          <el-input prefix-icon="el-icon-user" v-model.trim="loginForm.account"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码:" class="password" prop="password">
          <el-input
            prefix-icon="iconfont icon-mima"
            v-model.trim="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn_login">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button :plain="true" style="display:none">成功</el-button>
    </div>
  </div>
</template>

<script>
import Qs from "qs";
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        account: "",
        password: ""
      }
    };
  },
  methods: {
    // 点击重置账号密码
    reset() {
      this.$refs.loginFormRef.resetFields();
    },
    async login() {
      const { data: res } = await this.$http.login(this.loginForm);
      if (res.err == 0) {
        window.localStorage.setItem("token", res.res.token);
        window.localStorage.setItem("account", this.loginForm.account);
        window.localStorage.setItem("password", this.loginForm.password);
        this.$router.push("/home");
      }
    }
  },
  created() {
    let x = window.localStorage.getItem("account", this.loginForm.account);
    let y = window.localStorage.getItem("password", this.loginForm.password);
    if (x && y) {
      this.loginForm.account = x;
      this.loginForm.password = y;
    }
  }
};
</script>

<style lang="less" scoped>
.login_container {
  // background: linear-gradient(90deg, #f9a237 0%, #ffc74c 100%);
  background: url("../../src/assets/index.jpg") no-repeat center;
  background-size: cover;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    box-shadow: 0 0 10px #bdd;
    border-radius: 50%;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .login-form {
    position: absolute;
    bottom: 10px;
    width: 90%;
  }
  .btn_login {
    display: flex;
    justify-content: flex-end;
  }
}
</style>